/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare",
    "scprit/followup/app/datatable/datatable.servers.js",
    "scprit/followup/app/datatable/datatable.ui.js",],function($,$bb,$datatableServers,$datatableUi){
    var servers = {};
    var URL = $bb.baseUrl("followup");
    /**
     * 入参
     */
    var inParams = function(args){
        var data = JSON.stringify({
            data: {
                "args": JSON.stringify(args),
                "flag": 1,
                "patientName": $.trim($('#searchByName').val()),
                "hisNo": $.trim($('#searchByHisNo').val()),
                "phoneNum": $.trim($('#searchByPhone').val()),
            }
        });
        return data;
    }
    /**
     * 出参
     */
    var outParams = function(data){
        return data.rows;
    }
    /**
     * columns
     */
    var columnsHisNo = function(e){
        return e.hisNo1 ? e.hisNo1 : e.hisNo2 ? e.hisNo2 : e.hisNo3 ? e.hisNo3 : "-";
    }
    var columnsPatientName = function(e){
        var imgSrc = 'page/followup/imgs/comm/';
        imgSrc += e.gender == 'F' ? 'girl.png' : 'boy.png';
        var h = '<img class="pic-sex" src='+imgSrc+'>';
            h += '<span class="patient-name">' + e.patientName + '</span>'; 
        return h;
    }
    var columnsData = function(){
        var columnsArr = [columnsHisNo,columnsPatientName,'age','phoneNum','birthday','followQueueName','pregnancyWeek','dealTime',null];
        return $datatableServers.getColumnsData(columnsArr);
    }
    /**
     * 行绘制回调函数
     */
    var rowCallback = function(row, data, index) {
        var h = '<div class="btn-look">查看</div>';
        $('td:last()', row).html(h);
        $("td:last() .btn-look",row).unbind("click");
        $("td:last() .btn-look",row).click(function(){
            var params2 = {
                patientName: data.patientName,
                age: data.age,
                dealTime: data.dealTime,
            };
            var params1 = {
                patientId: data.patientId,
                queueId: data.followQueueId,
                taskId: data.taskId,
                type: "8",
                updateAccept: data.updateAccept,
            };
            $bb.openPage("complaintRegisterDetail",{params:params1,value:params2});
        });  
    };
    /**
     * 初始化完成之后的回调函数
     */
    var initComplete = function(setting,json){
        $("#complaintRegister").prev("div.row").children().first().removeClass("col-xs-6").addClass("col-xs-12");
        $("#complaintRegister").prev("div.row").children().last().remove();
        var h = '<div id="search" class="search">'+
                '<label>科研编号：'+
                '    <input id="searchByHisNo" type="text" class="form-control input-sm"></input>'+
                '</label>'+
                '<label>姓名：'+
                '    <input id="searchByName" type="text" class="form-control input-sm"></input>'+
                '</label>'+
                '<label>手机号：'+
                '    <input id="searchByPhone" type="text" class="form-control input-sm"></input>'+
                '</label>'+
                '</div>';
        $("#complaintRegister").prev("div.row").children().first().append(h);
        $("#searchByName, #searchByHisNo, #searchByPhone").bind("input propertychange",function(){
            $datatableUi.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatableUi.table.api().ajax.params();   //获取dt请求参数
        });  
        $("#complaintRegister_length select").change(function(){
            var length = $(this).val();
            $datatableUi.table.api().ajax.reload();  // 出发dt的重新加载数据的方法
            var args = $datatableUi.table.api().ajax.params();   //获取dt请求参数
            args = $.extend(args,{"length":length});    // 每页length个
        });
    }
    /**
     * 获取datatable里的options
     */
    servers.getOptions = function(){
        var sessionId = $bb.getSessionStorage("sessionId");
        var options = {
            "ajax": {
                "url": URL + "followup/service/patient/patientOnlineOperateListQuery?sessionId=" + sessionId,
                "data": inParams,
                "dataSrc": outParams
            },
            "columns": columnsData(),
            "rowCallback": rowCallback,
            "initComplete": initComplete,
        };
        return options;
    }

    return servers;
});